import React, { Component } from "react";
import { Link, Route, Switch } from "react-router-dom";

class Home extends Component {
  render() {
    // console.log(this.props.match?.params?.id);
    return <div>home页面</div>;
  }
}

class About extends Component {
  render() {
    // console.log(this);
    const {
      location: { search },
    } = this.props;
    // console.log(search);

    // 传的是search ?a=3&b=4
    const paramsObj = new URLSearchParams(search);
    console.log(paramsObj.get("a"));

    const obj = new URL("http://localhost:3000/about?a=3&b=4");
    console.log(obj.searchParams);

    return <div>about页面</div>;
  }
}

class Mine extends Component {
  render() {
    console.log(this.props);
    return <div>mine页面</div>;
  }
}

class App extends Component {
  render() {
    return (
      <>
        <h3>react-router-参数</h3>
        <ul>
          <li>
            {/* 动态路由传参 */}
            <Link to="/home/123">home</Link>
          </li>
          <li>
            {/* query传参 */}
            <Link to="/about?a=3&b=4">about</Link>
          </li>
          <li>
            {/* 对象的传参方式 */}
            {/* state是一种在url上面不可见的一种传参方式 */}
            <Link
              to={{
                pathname: "/mine",
                search: "?x=10&=20",
                hash: "#abc",
                state: { msg: "hello" },
              }}
            >
              mine
            </Link>
          </li>
        </ul>

        <hr />

        <Switch>
          <Route path="/home/:id" component={Home}></Route>
          <Route path="/about" component={About}></Route>
          <Route path="/mine" component={Mine}></Route>
        </Switch>
      </>
    );
  }
}

export default App;
